﻿using Cemit.NetFramework.Logger;
using Cemit.PolyProto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cemit.NetFramework
{
    partial class PolyNetwork
    {
        private static readonly GameClient gameClient = new GameClient();

        [System.Obsolete("使用FrameSyncRoom实例发送和获取操作帧消息")]
        public static bool SendOperationToGameRoom(O o)
        {
            if (!gameClient.IsConnected || !gameClient.IsLogined)
            {
                Debug.LogError("游戏服务器未连接或登录！");
                return false;
            }

            gameClient.SendOperation(o);
            return true;
        }

        /// <summary>加入游戏房间，失败返回null</summary>
        public static async Task<FrameSyncRoom> JoinGameRoom(GameRoomInfo roomInfo)
        {
            if (gameClient.IsConnected)
            {
                throw new Exception("当前已在游戏房间中！");
            }

            Debug.LogInfo($"尝试连接到大厅服务器：{roomInfo.Ip}:{roomInfo.Port}");

            var report = await gameClient.Connect(roomInfo.Ip, roomInfo.Port);

            if (!report.isSucceed)
            {
                Debug.LogError($"加入游戏房间失败：连接游戏服务器失败！{report.output}");
                return null;
            }

            LoginResult loginResult = await gameClient.Login(playerName);

            if (loginResult != LoginResult.Successful)
            {
                Debug.LogError($"加入游戏房间失败：登录游戏服务器失败！");
                return null;
            }

            return gameClient.InstancesFrameSyncRoom(roomInfo.RoomId);
        }
    }
}
